package com.example.wasaimovie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wasaimovie.Annotation.DisableBaseResponse;
import com.example.wasaimovie.Model.entity.*;
import com.example.wasaimovie.Model.support.ResponseResult;
import com.example.wasaimovie.Service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/api")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    UserService userService;

    @Autowired
    FilmService filmService;

    @Autowired
    OrderExceptionService orderExceptionService;

    @Autowired
    ArrangementService arrangementService;

    /**
     * 保存订单
     * @param order 订单信息
     * @return 是否保存成功
     */
    @PostMapping("/order")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> saveOrder(@RequestBody Order order){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("OrderController -> saveOrder");
        order.setId(UUID.randomUUID().toString());
        order.setCreateTime(format.format(new Date()));
        if (order.getStatus() == 2){
            order.setPayAt(format.format(new Date()));
        }
        boolean save = orderService.save(order);
        if (save){
            return new ResponseResult<>(200, null);
        } else {
            return new ResponseResult<>(400, "保存失败");
        }
    }

    /**
     * 获取订单页面
     * @param id 用户id
     * @return 该用户所有订单信息
     */
    @GetMapping("/order/user/{id}")
    @CrossOrigin
    public List<Map<String, Object>> getMyOrder(@PathVariable String id){
        List<Map<String, Object>> orderById = orderService.getOrderById(id);
        return orderById;
    }

    /**
     * 支付订单
     * @param id 订单id
     * @return 是否成功
     */
    @GetMapping("/order/pay/{id}")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> payment(@PathVariable String id){
        return new ResponseResult<>(200, null);
    }

    /**
     * 获取所有订单
     * @return 订单信息
     */
    @GetMapping("/order")
    @CrossOrigin
    public List<Map<String, Object>> getAllOrder(){
        List<Order> orderList = orderService.list();
        List<Map<String, Object>> result = new ArrayList<>();
        for (Order order : orderList) {
            Map<String, Object> map = new HashMap<>();
            map.put("order", order);

            // 查询用户
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id", order.getUid());
            List<User> list = userService.list(userQueryWrapper);
            if (!list.isEmpty()){
                map.put("user", list.get(0));
            }

            // 查询排片信息
            QueryWrapper<Arrangement> arrangementQueryWrapper = new QueryWrapper<>();
            arrangementQueryWrapper.eq("id", order.getAid());
            List<Arrangement> arrangementList = arrangementService.list(arrangementQueryWrapper);
            if (!arrangementList.isEmpty()){
                // 查询结果不为空
                // 查询电影
                QueryWrapper<Film> filmQueryWrapper = new QueryWrapper<>();
                filmQueryWrapper.eq("id", arrangementList.get(0).getFid());
                List<Film> filmList = filmService.list(filmQueryWrapper);
                if (!filmList.isEmpty()){
                    map.put("film", filmList.get(0));
                }
                map.put("arrangement", arrangementList.get(0));
                result.add(map);
            }

        }

        return result;
    }

    /**
     * 根据ID修改订单信息
     * @param order 更新后的信息
     * @return 是否修改成功
     */
    @PutMapping("/order")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> updateOrder(@RequestBody Order order){
        System.out.println(order);
        if (orderService.updateById(order)){
            return new ResponseResult<>(200, null);
        }
        return new ResponseResult<>(400, "修改失败");
    }

    /**
     * 添加异常订单
     * @param orderException 订单信息
     * @return 是否添加成功
     */
    @PostMapping("/oe")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> addErrOrder(@RequestBody OrderException orderException){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        orderException.setId(UUID.randomUUID().toString());
        orderException.setStatus(0);
        orderException.setCreateTime(format.format(new Date()));
        if (orderExceptionService.save(orderException)){
            return new ResponseResult<>(200, null);
        }
        return new ResponseResult<>(400, "添加失败");
    }

    /**
     * 获取所有订单信息
     * @return 订单信息
     */
    @GetMapping("/oe")
    @CrossOrigin
    public List<OrderException> getAllErr(){
        return orderExceptionService.list();
    }

    /**
     * 处理异常订单
     * @param orderException 异常订单信息
     * @return 是否处理成功
     */
    @PutMapping("/oe")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> handleErr(@RequestBody OrderException orderException){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        orderException.setEndAt(format.format(new Date()));
        if (orderExceptionService.updateById(orderException)){
            return new ResponseResult<>(200, null);
        }

        return new ResponseResult<>(400, "处理失败");
    }

}
